#include "editorgallery.h"

#include "editorgallerygroup.h"
#include "gallerycommon.h"

#include <ieditor.h>

#include <QtGui/QScrollArea>
#include <QtGui/QVBoxLayout>

using namespace core;
using namespace gui;

EditorGallery::EditorGallery(QWidget* parent) : QScrollArea(parent),
    m_layout(0),
    m_scrollAreaWidget(0)
{
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    setWidgetResizable(true);

    m_scrollAreaWidget = new QFrame(this);
    m_scrollAreaWidget->setSizePolicy(QSizePolicy::MinimumExpanding,
        QSizePolicy::Preferred);

    m_layout = new QVBoxLayout(m_scrollAreaWidget);
    m_scrollAreaWidget->setLayout(m_layout);

    setWidget(m_scrollAreaWidget);

    setSizePolicy(QSizePolicy::MinimumExpanding,
        QSizePolicy::MinimumExpanding);
}

EditorGallery::~EditorGallery()
{

}

// public slots
void EditorGallery::refreshGallery(
    const QSharedPointer<IEditor> &editor)
{
    QLayoutItem* item;
    while ((item = m_layout->takeAt(0)) != 0)
    {
        delete item->widget();
        delete item;
    }

    if (!editor.isNull())
    {
        foreach (const QString& group, editor->galleryGroups())
        {
            EditorGalleryGroup* galleryGroup = new EditorGalleryGroup(group,
                editor->galleryItems(group));

            m_layout->addWidget(galleryGroup);
        }

        m_layout->addStretch();
    }
}
